package com.feng.test.ppt;

import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.sl.usermodel.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.UUID;

/**
 * @author HuangSuip
 */
public class PPTAnalyse {
    public static void main(String[] args) {
        try (InputStream inputStream = new FileInputStream(new File("E:\\ppt\\化学22.ppt"))) {
            HSLFSlideShow slideShow = new HSLFSlideShow(inputStream);
            Dimension dimension = slideShow.getPageSize();
            List<HSLFSlideMaster> masterList = slideShow.getSlideMasters();
            for (HSLFSlideMaster master : masterList) {
                HSLFBackground background = master.getBackground();
                HSLFFill fill = background.getFill();
                Color color = fill.getForegroundColor();
                HSLFPictureData data = fill.getPictureData();
                System.out.println("Color: " + color + "   " + data);


                //master(dimension, master);
            }

            List<HSLFSlide> slideList = slideShow.getSlides();
            int i = 1;
            for (HSLFSlide slide : slideList) {
                System.out.println("++++++++++第" + i++ + "张+++++++");
                System.out.println("MasterBackground: " + slide.getFollowMasterBackground());
                HSLFMasterSheet sheet = slide.getMasterSheet();
                HSLFBackground background1 = slide.getBackground();

                HSLFBackground background = sheet.getBackground();
                HSLFFill fill = background1.getFill();
                Color color = fill.getForegroundColor();
                HSLFPictureData data = fill.getPictureData();
                System.out.println("PIC date: " + data);
                System.out.println("Color: " + color);
                List<HSLFShape> shapeList = slide.getShapes();
                drawSlide(slide);
                for (HSLFShape shape : shapeList) {
                    //shapeALl(shape);
                    if (shape instanceof HSLFTextShape) {
                        //textShape(shape);
                    }
                    if (shape instanceof HSLFConnectorShape) {
                        //connectorShape((HSLFConnectorShape) shape);
                    }
                    if (shape instanceof HSLFTable) {
                        //tableAnalyse((HSLFTable) shape);
                        //tableProps((HSLFTable) shape);
                    }
                    if (shape instanceof HSLFLine) {
                        //lineAnalyse((HSLFLine) shape);
                    }
                    if (shape instanceof HSLFGroupShape) {

                        HSLFGroupShape groupShape = (HSLFGroupShape) shape;
                        List<HSLFShape> shapeList1 = groupShape.getShapes();
                        for (HSLFShape hslfShape : shapeList1){
                            //System.out.println(hslfShape.getClass());
                        }
                        //drawShape(shape);
                    }
                    if (shape instanceof HSLFFreeformShape) {
                        //freeformAnalyse((HSLFFreeformShape) shape);
                        //drawShape(shape);
                    }
                    if (shape instanceof HSLFAutoShape) {
                        //autoShape((HSLFAutoShape) shape);

                        //drawShape(shape);
                    }
                    System.out.println();
                }
                //drawSlide(slide);
                //master(dimension, slide);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void tableProps(HSLFTable shape) {


    }

    private static void master(Dimension dimension, HSLFSlideMaster master) {
        BufferedImage image = new BufferedImage((int) dimension.getWidth(), (int) dimension.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = image.createGraphics();
        Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, dimension.getWidth(), dimension.getHeight());
        graphics2D.fill(rectangle2D);
        master.draw(graphics2D);
        //background.draw(graphics2D,rectangle2D);
        try (FileOutputStream fos = new FileOutputStream(new File("E:\\ppt\\slide\\" + UUID.randomUUID() + ".jpg"))) {
            ImageIO.write(image, "png", fos);
            System.out.println("pic draw success");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void master(Dimension dimension, HSLFSlide slide) {
        HSLFMasterSheet masterSheet = slide.getMasterSheet();

        BufferedImage image = new BufferedImage((int) dimension.getWidth() + 1, (int) dimension.getHeight() + 1, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics2D = image.createGraphics();
        graphics2D.fill(new Rectangle2D.Double(0, 0, dimension.getWidth(), dimension.getHeight()));
        //图片旋转
        Rectangle2D rectangle2D1 = new Rectangle2D.Double(0, 0, dimension.getWidth(), dimension.getHeight());
        //slide.getBackground().draw(graphics2D,rectangle2D1);
        masterSheet.draw(graphics2D);
        try (FileOutputStream fos = new FileOutputStream(new File("E:\\ppt\\slide\\" + UUID.randomUUID() + ".jpg"))) {
            ImageIO.write(image, "png", fos);
            System.out.println("pic is draw success");
        } catch (IOException e) {
            e.printStackTrace();
        }

        HSLFBackground background2 = slide.getBackground();
        HSLFFill fill2 = background2.getFill();
        Color color2 = fill2.getForegroundColor();
        HSLFPictureData data2 = fill2.getPictureData();
        System.out.println("Color2: " + color2 + "   " + data2);
    }

    private static void autoShape(HSLFAutoShape shape) {
        System.out.println(shape.getText());
    }

    private static void freeformAnalyse(HSLFFreeformShape shape) {
        System.out.println(shape.getFillColor());
    }

    private static void shapeALl(HSLFShape shape) {
        System.out.println("shapeName: " + shape.getShapeName() + "  " + shape.getClass());
        System.out.println("shapeType: " + shape.getShapeType());
        System.out.println("Height: "+shape.getAnchor().getHeight());
        System.out.println("Width: "+shape.getAnchor().getWidth());
        System.out.println("Top: "+shape.getAnchor().getX());
        System.out.println("Left: "+shape.getAnchor().getY());
    }

    private static void drawSlide(HSLFSlide slide) {
        Dimension dimension = slide.getSlideShow().getPageSize();
        BufferedImage image = new BufferedImage((int) dimension.getWidth() + 1, (int) dimension.getHeight() + 1, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics2D = image.createGraphics();
        graphics2D.fill(new Rectangle2D.Double(0, 0, dimension.getWidth(), dimension.getHeight()));
        //图片旋转
        slide.draw(graphics2D);
        try (FileOutputStream fos = new FileOutputStream(new File("E:\\ppt\\slide\\" + UUID.randomUUID() + ".jpg"))) {
            ImageIO.write(image, "png", fos);
            System.out.println("pic is draw success");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void lineAnalyse(HSLFLine shape) {

    }

    private static void tableAnalyse(HSLFTable table) {
        StringBuilder sb = new StringBuilder();
        sb.append("<table>");
        for (int i = 0; i < table.getNumberOfRows(); i++) {
            sb.append("<tr style='");
            sb.append("'>");
            for (int j = 0; j < table.getNumberOfColumns(); j++) {
                HSLFTableCell cell = table.getCell(i, j);
                if (cell == null) {
                    System.out.println("is null "+"rows: "+ i + " ,columns: "+ j);
                    continue;
                }
                if (cell.isMerged()){
                    System.out.println("is merged "+"rows: "+ i + " ,columns: "+ j);
                }
/*                System.out.println("第" + i + "个单元格: span:" + cell.getRowSpan());
                System.out.println("单元格跨度：" + cell.getGridSpan());
                System.out.println("单元格文字：" + cell.getText());
                System.out.println("单元格文字方向：" + cell.getVerticalAlignment());
                System.out.println("单元格宽高：" + cell.getAnchor().getWidth() + "   " + cell.getAnchor().getHeight());*/

                sb.append("<td style='");
                Rectangle2D rectangle2D = cell.getAnchor();
                sb.append("width:" + rectangle2D.getWidth() + "px;" + "height:" + rectangle2D.getHeight() + "px;");

                StrokeStyle rightStrokeStyle = cell.getBorderStyle(TableCell.BorderEdge.right);
                StrokeStyle leftStrokeStyle = cell.getBorderStyle(TableCell.BorderEdge.left);
                StrokeStyle botStrokeStyle = cell.getBorderStyle(TableCell.BorderEdge.bottom);
                StrokeStyle topStrokeStyle = cell.getBorderStyle(TableCell.BorderEdge.top);
                if (rightStrokeStyle != null) {
                    Color color = ((PaintStyle.SolidPaint) rightStrokeStyle.getPaint()).getSolidColor().getColor();
                    sb.append("border-right:3px solid rgb(" + color.getRed() + "," + color.getGreen() + "," + color.getBlue() + ");");
                }
                if (leftStrokeStyle != null) {
                    Color color = ((PaintStyle.SolidPaint) leftStrokeStyle.getPaint()).getSolidColor().getColor();
                    sb.append("border-left:3px solid rgb(" + color.getRed() + "," + color.getGreen() + "," + color.getBlue() + ");");
                }
                if (botStrokeStyle != null) {
                    Color color = ((PaintStyle.SolidPaint) botStrokeStyle.getPaint()).getSolidColor().getColor();
                    sb.append("border-bottom:3px solid rgb(" + color.getRed() + "," + color.getGreen() + "," + color.getBlue() + ");");
                }
                if (topStrokeStyle != null) {
                    Color color = ((PaintStyle.SolidPaint) topStrokeStyle.getPaint()).getSolidColor().getColor();
                    sb.append("border-top:3px solid rgb(" + color.getRed() + "," + color.getGreen() + "," + color.getBlue() + ");");
                }
                sb.append("' colspan='" + cell.getGridSpan() + "' rowspan='" + cell.getRowSpan() + "' >");

                List<HSLFTextParagraph> paragraphs = cell.getTextParagraphs();
                for (TextParagraph paragraph : paragraphs) {
                    List<TextRun> textRuns = paragraph.getTextRuns();
                    for (TextRun textRun : textRuns) {
                        if (true) {
                            sb.append("<span style='");
                            //拼接颜色
                            if (textRun.getFontColor() instanceof PaintStyle.SolidPaint) {
                                Color color = ((PaintStyle.SolidPaint) textRun.getFontColor()).getSolidColor().getColor();
                                sb.append("color:rgb(" + color.getRed() + "," + color.getGreen() + "," + color.getBlue() + ");");
                            }
                            //加粗
                            if (textRun.isBold()) {
                                sb.append("font-weight:bold;");
                            }
                            if (textRun.isItalic()) {
                                sb.append("font-style:italic;");
                            }
                            if (textRun.isUnderlined()) {
                                sb.append("text-decoration:underline;");
                            }
                            //字体
                            sb.append("font-family:" + textRun.getFontFamily() + ";");
                            //大小
                            sb.append("font-size:" + textRun.getFontSize() + "pt;");
                            sb.append("'>");
                            sb.append(textRun.getRawText());
                            sb.append("</span>");
                        }
                    }
                }

                sb.append("</td>");
            }
            sb.append("</tr>");
        }
        sb.append("</table>");
        System.out.println(sb.toString());
    }

    private static void connectorShape(HSLFConnectorShape shape) {
        System.out.println(shape.getShapeName());
    }

    private static void textShape(HSLFShape shape) {

        if (shape instanceof HSLFFreeformShape) {
            freeformShape((HSLFFreeformShape) shape);
        }
    }

    private static void freeformShape(HSLFFreeformShape shape) {
        System.out.println(shape);
    }

    private static void drawShape(HSLFShape shape) {
        Rectangle2D rectangle2D = shape.getAnchor();
        BufferedImage image = new BufferedImage((int) rectangle2D.getWidth() + 1, (int) rectangle2D.getHeight() + 1, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics2D = image.createGraphics();
        graphics2D.fill(new Rectangle2D.Double(0, 0, rectangle2D.getWidth(), rectangle2D.getHeight()));
        //图片旋转
        Rectangle2D rectangle2D1 = new Rectangle2D.Double(0, 0, rectangle2D.getWidth(), rectangle2D.getHeight());
        shape.draw(graphics2D, rectangle2D1);
        try (FileOutputStream fos = new FileOutputStream(new File("E:\\ppt\\slide\\" + UUID.randomUUID() + ".jpg"))) {
            ImageIO.write(image, "png", fos);
            System.out.println("pic is draw success");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
